<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ActivateController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    public function showLb()
    {
        return view('admin.activate.lb');
    }

    /**
     * 获取老兵活动列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getLbList(Request $request)
    {
        $w = [];
        if ($request->get('unionid', '')) {
            $w[] = ['unionid', '=', $request->get('unionid', '')];
        }
        $list = DB::table('activate_user_lb')
            ->where($w)
            ->orderBy('id', 'desc')
            ->paginate(10);
        $levelMap = DB::table('user_level')->pluck('title', 'id');
        foreach ($list as &$item) {
            $item->level_title = $levelMap[$item->level_id] ?? '';
        }
        return successResp($list);
    }

    /**
     * 获取老兵活动领取记录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getLbGetLog(Request $request)
    {
        $id = $request->get('id', 0);
        if ($id <= 0) {
            return errorResp('id empty');
        }
        $list = DB::table('activate_user_lb_log')
            ->where('activate_id', $id)
            ->orderBy('id', 'desc')
            ->get();
        return successResp($list);
    }

    public function showFiveDay()
    {
        return view('admin.activate.five');
    }

    /**
     * 获取五天活动列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getFiveDayList(Request $request)
    {
        $w = [];
        if ($request->get('unionid', '')) {
            $w[] = ['unionid', '=', $request->get('unionid', '')];
        }
        if ($request->get('status', '')) {
            $w[] = ['status', '=', $request->get('status', '')];
        }
        $list = DB::table('activate_level_five_day')
            ->where($w)
            ->orderBy('id', 'desc')
            ->paginate(10);
        $levelMap = DB::table('user_level')->pluck('title', 'id');
        foreach ($list as &$item) {
            $item->level_title = $levelMap[$item->level_id] ?? '';
            if ($item->status != 2 && strtotime($item->expired_time) < time()) {
                $item->status = 3; // 已过期
            }
        }
        return successResp($list);
    }
}
